package com.ejoysoft.etable.system;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Vector;

import com.ejoysoft.common.DbConnect;
import com.ejoysoft.common.Format;
import com.ejoysoft.common.Globa;
import com.ejoysoft.common.UID;

public class TagConf {

	private Globa globa;
    private DbConnect db;

    //���췽��
    public TagConf() {
    }

    //���췽��
    public TagConf(Globa globa, boolean b) {
        this.globa = globa;
        db = globa.db;
        if (b) globa.setDynamicProperty(this);
    }

    static String strTableName = "t_tagConf";
    
    //���
    public boolean add() {
        String strSql = "";
        try {
            strId = UID.getID();
            strSql = "INSERT INTO " + strTableName + " (strId,strTagName,strParentId,strType," +
            		"strTable,strColumn,strCreator,dCreatDate,strRemark,strState,intLevel,intSort) " +
            		"VALUES (?,?,?,?,?,?,?,?,?,?,?,?) ";
            db.prepareStatement(strSql);
            db.setString(1, strId);
            db.setString(2, strTagName);
            db.setString(3, strParentId);
            db.setString(4, strType);
            db.setString(5, strTable);
            db.setString(6, strColumn);
            db.setString(7, globa.loginName);
            db.setString(8, com.ejoysoft.common.Format.getDateTime());
            db.setString(9, strRemark);
            db.setString(10, "��");
            db.setInt(11, strParentId.equals("0") ? 1 : hmTag.get(strParentId).getIntLevel() + 1);
            db.setInt(12, strParentId.equals("0") ? vctRoot.size() + 1 : hmTag.get(strParentId).getChildren().size() + 1);
            if (db.executeUpdate() > 0) {
                Globa.logger0("���ָ����Ϣ", globa.loginName, globa.loginIp, strSql, "ָ�����", globa.userSession.getStrDepart());
                return true;
            } else
                return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    //�ı�sort���ݹ�ı��ӽڵ�
    private void changeCode(TagConf tag, String strNewCode) {
    	String sql = "UPDATE " + strTableName + " SET strCode='";
    }

    //ɾ��
    public boolean delete(String where) {
        try {
        	// ά���ֵܽڵ����
        	TagConf tg = hmTag.get(strId);
        	String sql = "UPDATE " + strTableName + " SET intSort=intSort-1 WHERE strParentId='" + 
        	             tg.getStrParentId() + "' AND intSort>" + tg.getIntSort();
    		db.executeUpdate(sql);
    		//ɾ��
            sql = "DELETE FROM " + strTableName + "  ".concat(where);
            db.executeUpdate(sql);
            Globa.logger0("ɾ��ָ����Ϣ", globa.loginName, globa.loginIp, sql, "ָ�����", globa.userSession.getStrDepart());
            return true;
        } catch (Exception ee) {
            ee.printStackTrace();
            return false;
        }
    }

    //�޸�
    public boolean update(String tStrId) {
        try {
        	String sql;
        	if (!this.strParentId.equals(this.strOldParentId)) {	//�ı������¼���ϵ��ά�����
        		// ά��ԭ4���ֵܽڵ����
            	sql = "UPDATE " + strTableName + " SET intSort=intSort-1 WHERE strParentId='" + 
            	             strOldParentId + "' AND intSort>" + intOldSort;
        		db.executeUpdate(sql);
        		// ά���µ��ֵܽڵ����
        		sql = "UPDATE " + strTableName + " SET intSort=intSort+1 WHERE strParentId='" + 
	             	  strParentId + "' AND intSort>=" + intSort;
        		db.executeUpdate(sql);
        	} else if (this.intSort != this.intOldSort) {	//�ı���ţ�ά���ֵܽڵ����
        		if (this.intOldSort < this.intSort) {
        			sql = "UPDATE " + strTableName + " SET intSort=intSort-1 WHERE strParentId='" + 
        				  strParentId + "' AND intSort>" + intOldSort + " AND intSort<=" + intSort;
        		} else {
        			sql = "UPDATE " + strTableName + " SET intSort=intSort+1 WHERE strParentId='" + 
					 	  strParentId + "' AND intSort<" + intOldSort + " AND intSort>=" + intSort;
        		}
        		db.executeUpdate(sql);
        	}
            sql = "UPDATE  " + strTableName + "  SET strTagName=?,strParentId=?,strType=?," +
            		"strTable=?,strColumn=?,strCreator=?,dCreatDate=?,strRemark=?,strState=?,intLevel=?," +
            		"intSort=?  WHERE strId = ? ";
            db.prepareStatement(sql);
            db.setString(1, strTagName);
            db.setString(2, strParentId);
            db.setString(3, strType);
            db.setString(4, strTable);
            db.setString(5, strColumn);
            db.setString(6, globa.loginName);
            db.setString(7, com.ejoysoft.common.Format.getDateTime());
            db.setString(8, strRemark);
            db.setString(9, strState);
            db.setInt(10, strParentId.equals("0") ? 1 : hmTag.get(strParentId).getIntLevel() + 1);
            db.setInt(11, intSort);
            db.setString(12, tStrId);
            db.executeUpdate();
            Globa.logger0("�޸�ָ����Ϣ", globa.loginName, globa.loginIp, sql, "ָ�����", globa.userSession.getStrDepart());
            return true;
        } catch (Exception e) {
            System.out.println("�޸�ָ����Ϣʱ��?" + e);
            return false;
        }
    }

    //��ϸ��ʾ�����¼
    public TagConf show(String where) {
        try {
            String strSql = "select * from  " + strTableName + "  ".concat(where);
            ResultSet rs = db.executeQuery(strSql);
            if (rs != null && rs.next())
                return load(rs, true);
            else
                return null;
        } catch (Exception ee) {
            return null;
        }
    }

    //��¼��ת��Ϊ����
    public TagConf load(ResultSet rs, boolean isView) {
    	TagConf theBean = new TagConf();
        try {
            theBean.setStrId(rs.getString("strId"));
            theBean.setStrTagName(rs.getString("strTagName"));
            theBean.setStrParentId(rs.getString("strParentId"));
            theBean.setStrType(rs.getString("strType"));
            theBean.setStrTable(rs.getString("strTable"));
            theBean.setStrColumn(rs.getString("strColumn"));
            theBean.setStrCreator(rs.getString("strCreator"));
            theBean.setdCreatDate(rs.getString("dCreatDate"));
            theBean.setStrRemark(rs.getString("strRemark"));
            theBean.setStrState(rs.getString("strState"));
            theBean.setIntLevel(rs.getInt("intLevel"));
            theBean.setIntSort(rs.getInt("intSort"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return theBean;
    }

    //��ѯ�������ļ�¼����
    public int getCount(String where) {
        int count = 0;
        try {
            String sql = "SELECT count(*) FROM " + strTableName + "  ";
            if (where.length() > 0) {
                where = where.toLowerCase();
                if (where.indexOf("order") > 0)
                    where = where.substring(0, where.lastIndexOf("order"));
                sql = String.valueOf(sql) + String.valueOf(where);
            }
            ResultSet rs = db.executeQuery(sql);
            if (rs.next())
                count = rs.getInt(1);
            rs.close();
            return count;
        } catch (Exception ee) {
            ee.printStackTrace();
            return count;
        }
    }

    private String strId;//	id��
    private String strTagName;//	���
    private String strParentId;//		 	�ϼ�ID
    private String strType;	// ���ͣ���ָ�����ͣ�ָ��
    private String strTable;	// ����
    private String strColumn;	// ����ָ������Ϊ��
    private String strCreator;//			������
    private String dCreatDate;//		����ʱ��
    
    private String strOldParentId;	//ԭ�еĸ�ָ��ID�����ڸ��²���
    private int intOldSort;	//ԭ�е�˳��ţ����ڸ��²���
    private int intSort;	//ͬ���е�˳��ţ���1��ʼ
    private int intLevel;	// �㼶����1��ʼ
    private String strRemark;	//��ע
    private String strState;	//״̬�������
    
    public static HashMap<String, TagConf> hmTag;	//����ָ���ϣ��
    public static Vector<TagConf> vctTagTree;	//����ָ������νṹ
    public static Vector<TagConf> vctRoot;	//���ж���ָ��
    
    public static void init() {
    	hmTag = new HashMap<String, TagConf>();
    	vctTagTree = new Vector<TagConf>();
    	vctRoot = new Vector<TagConf>();
    	String sql = "SELECT * FROM " + strTableName + " ORDER BY strParentId,intSort";
    	try {
    		Connection con = DbConnect.getStaticCon();
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
            	TagConf theBean = new TagConf();
            	theBean.setStrId(rs.getString("strId"));
                theBean.setStrTagName(rs.getString("strTagName"));
                theBean.setStrParentId(rs.getString("strParentId"));
                theBean.setStrType(rs.getString("strType"));
                theBean.setStrTable(rs.getString("strTable"));
                theBean.setStrColumn(rs.getString("strColumn"));
                theBean.setStrCreator(rs.getString("strCreator"));
                theBean.setdCreatDate(rs.getString("dCreatDate"));
                theBean.setStrRemark(rs.getString("strRemark"));
                theBean.setStrState(rs.getString("strState"));
                theBean.setIntLevel(rs.getInt("intLevel"));
                theBean.setIntSort(rs.getInt("intSort"));
                TagConf parent = hmTag.get(theBean.getStrParentId());
                if (parent != null) {
                	parent.addChild(theBean);
                } else {
                	vctRoot.add(theBean);
                }
                hmTag.put(theBean.getStrId(), theBean);
            }
            createTree(vctTagTree, vctRoot);
            System.out.println("----------------��ʼ��ָ��ɹ���");
            rs.close();
            stmt.close();
            con.close();
    	} catch (Exception e) {
    		e.printStackTrace();
    		System.out.println("----------------��ʼ��ָ��ʧ�ܣ�����");
    	}
    }
    
    //������δ���
    private static void createTree(Vector<TagConf> tree, Vector<TagConf> root) {
    	for (int i = 0; i < root.size(); i++) {
    		tree.add(root.get(i));
    		createTree(tree, root.get(i).getChildren());
    	}
    }
    
    public String getStrId() {
        return strId;
    }

    public void setStrId(String strId) {
        this.strId = strId;
    }

	public void setStrTagName(String strTagName) {
		this.strTagName = strTagName;
	}

	public String getStrTagName() {
		return strTagName;
	}

	public String getStrParentId() {
        return Format.removeNull(strParentId);
    }

    public void setStrParentId(String strParentId) {
        this.strParentId = strParentId;
    }

    public void setStrType(String strType) {
		this.strType = strType;
	}

	public String getStrType() {
		return strType;
	}

	public void setStrTable(String strTable) {
		this.strTable = strTable;
	}

	public String getStrTable() {
		return strTable;
	}

	public void setStrColumn(String strColumn) {
		this.strColumn = strColumn;
	}

	public String getStrColumn() {
		return strColumn;
	}

	public String getStrCreator() {
        return strCreator;
    }

    public void setStrCreator(String strCreator) {
        this.strCreator = strCreator;
    }

    public String getdCreatDate() {
        return dCreatDate;
    }

    public void setdCreatDate(String dCreatDate) {
        this.dCreatDate = dCreatDate;
    }

    public void setIntLevel(int intLevel) {
		this.intLevel = intLevel;
	}

	public int getIntLevel() {
		return intLevel;
	}

	public void setStrOldParentId(String strOldParentId) {
		this.strOldParentId = strOldParentId;
	}

	public String getStrOldParentId() {
		return strOldParentId;
	}

	private Vector<TagConf> children = new Vector<TagConf>(); //�¼���

    public Vector<TagConf> getChildren() {
        return children;
    }

    public void setChildren(Vector<TagConf> children) {
        this.children = children;
    }

    public void addChild(TagConf tag) {
        this.children.add(tag);
    }

    /**
     * �ж��Ƿ����¼��û���
     * @return
     */
    public boolean haveChild() {
        return this.children.size() > 0;
    }

	public void setStrRemark(String strRemark) {
		this.strRemark = strRemark;
	}

	public String getStrRemark() {
		return strRemark;
	}

	public void setStrState(String strState) {
		this.strState = strState;
	}

	public String getStrState() {
		return strState;
	}

	public void setIntOldSort(int intOldSort) {
		this.intOldSort = intOldSort;
	}

	public int getIntOldSort() {
		return intOldSort;
	}

	public void setIntSort(int intSort) {
		this.intSort = intSort;
	}

	public int getIntSort() {
		return intSort;
	}
}
